﻿using AutoMapper;
using MediatR;
using Production_Set.API.MaterialManagement.Add;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.OutboundOrder;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Update
{
    public class OutBoundUpdateHandler : IRequestHandler<OutBoundUpdateCommon, Result>
    {
        private readonly IBaseRepository<OutBound> OutBound;
        public IMapper mapper { get; set; }
        public OutBoundUpdateHandler(IBaseRepository<OutBound> outBound , IMapper mapper)
        {
            OutBound = outBound;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(OutBoundUpdateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<OutBound>(request);
            string sql = "UPDATE OutBound SET BoundId=@BoundId,BoundNo=@BoundNo,BoundType=@BoundType ,WarehouseId=@WarehouseId ,WarehouseAddress=@WarehouseAddress " +
                         ",BoundRemark=@BoundRemark,State=@State,BoundSource=@BoundSource,SourceNo=@SourceNo,SourceName=@SourceName," +
                         "Customer=@Customer,Contacts=@Contacts,BoundDate=@BoundDate,BoundState=@BoundState,BoundPeople=@BoundPeople," +
                         "BoundDepartment=@BoundDepartment,Handle=@Handle,HandleContext=@HandleContext,HandleTime=@HandleTime,Pid=@Pid,IsDelete=@IsDelete WHERE BoundId =@BoundId";
            var AddNum = OutBound.Add(sql, new
            {
                BoundId=AddData.BoundId,
                BoundNo=AddData.BoundNo,
                BoundType=AddData.BoundType,
                WarehouseId = AddData.WarehouseId,
                WarehouseAddress = AddData.WarehouseAddress,
                BoundRemark = AddData.BoundRemark,
                State = AddData.State,
                BoundSource=request.BoundSource,
                SourceNo = AddData.SourceNo,
                SourceName = AddData.SourceName,
                Customer = AddData.Customer,
                Contacts = AddData.Contacts,
                BoundDate = request.BoundDate,
                BoundState = AddData.BoundState,
                BoundPeople = AddData.BoundPeople,
                BoundDepartment = AddData.BoundDepartment,
                Handle = AddData.Handle,
                HandleContext = AddData.HandleContext,
                HandleTime = AddData.HandleTime,
                Pid = AddData.Pid,
                IsDelete = AddData.IsDelete,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
